<template>
    <el-menu
        class="common-side-bar"
        :default-active="onRoutes"
        background-color="#2e3138"
        text-color="#BFCBD9"
        active-text-color="#1dbe89"
        @select="select"
        unique-opened
        :collapse="false"
    >
        <el-menu-item index="DashBord" @click="$store.state.headTitle = 'FasterRunner 接口自动化测试平台'">
            <i class="el-icon-s-home"></i>
            首页
        </el-menu-item>

        <el-menu-item index="ProjectList" @click="$store.state.headTitle = 'FasterRunner 接口自动化测试平台'">
            <i class="el-icon-tickets"></i>
            项目列表
        </el-menu-item>

        <el-menu-item index="PJ_DebugTalk">
            <i class="el-icon-s-opportunity"></i>
            平台驱动
        </el-menu-item>

        <el-menu-item v-for="item of side_menu" :index="item.url" :key="item.url" :disabled="routed === 'ProjectList' || routed === 'PJ_DebugTalk' || routed === 'DashBord'  || routed === 'Index'  || pid ===0 ">
            <i :class="item.code"></i>
            {{item.name}}
        </el-menu-item>
        <el-submenu index="1" :disabled="routed === 'ProjectList' || routed === 'PJ_DebugTalk'|| routed === 'DashBord' || routed === 'Index' || pid ===0 ">
            <template slot="title">
                <i class="el-icon-caret-right"></i>
                执行用例
            </template>
            <el-menu-item class="submenu-side-bar" v-for="item of run_menu" :index="item.url" :key="item.url">
                <i :class="item.code"></i>
                {{item.name}}
            </el-menu-item>
        </el-submenu>
        <el-submenu index="2" :disabled="routed === 'ProjectList' || routed === 'PJ_DebugTalk'|| routed === 'DashBord' || routed === 'Index' || pid ===0 ">
            <template slot="title">
                <i class="el-icon-s-tools"></i>
                参数配置
            </template>
            <el-menu-item class="submenu-side-bar" v-for="item of config_menu" :index="item.url" :key="item.url">
                <i :class="item.code"></i>
                {{item.name}}
            </el-menu-item>
        </el-submenu>

        <el-submenu index="3" :disabled="routed === 'ProjectList' || routed === 'PJ_DebugTalk'|| routed === 'DashBord' || routed === 'Index' || pid ===0 ">
            <template slot="title">
                <i class="el-icon-folder-opened"></i>
                报告回执
            </template>
            <div class="submenu-side-bar">
                <el-menu-item class="submenu-side-bar" v-for="item of report_menu" :index="item.url" :key="item.url">
                    <i :class="item.code"></i>
                    {{item.name}}
                </el-menu-item>
            </div>
        </el-submenu>
    </el-menu>
</template>

<script>
    export default {
        name: "Side",
        data() {
            return {
                routed: '',
                side_menu: [
                    {name: "项目概况", url: "ProjectDetail", code: "el-icon-data-board"},
                    {name: "测试数据", url: "TestData", code: "el-icon-s-data"},
                    {name: "项目驱动", url: "DebugTalk", code: "el-icon-s-opportunity"},
                ],
                run_menu: [
                    {name: "API 模板", url: "RecordApi", code: "el-icon-document"},
                    {name: "测试用例", url: "AutoTest", code: "el-icon-suitcase"},
                    {name: "定时任务", url: "Task", code: "el-icon-alarm-clock"}
                ],
                config_menu: [
                    {name: "配置管理", url: "RecordConfig", code: "el-icon-setting"},
                    {name: "全局变量", url: "GlobalEnv", code: "el-icon-cloudy"},
                    {name: "域名管理", url: "HostIP", code: "el-icon-news"}
                ],
                report_menu: [
                    {name: "历史报告", url: "Reports", code: "el-icon-reading"},
                    {name: "异步回执", url: "TaskMeta", code: "el-icon-pie-chart"}
                ],
                pid:0
            }
        },
        methods: {
            select(url) {
                console.log("切换左侧菜单:",url);
                this.$store.commit('setRouterName', url);
                this.$router.push({name: url});
                this.setLocalValue("routerName", url);
            }
        },
        computed: {
            onRoutes() {
                return this.routed
            },
        },
        watch: {
            $route(newValue, oldValue) {
                console.log("监测router，new_value:", newValue.name);
                if(newValue.name==='DashBord' || newValue.name==='ProjectList'|| newValue.name==='PJ_DebugTalk'){
                    this.$store.commit('setPid', 0);
                }
                this.routed = newValue.name;
                this.pid = this.$store.state.pid;
                console.log("project_id:",this.pid)
            }
        },
        created() {
            this.routed = this.$route.name
        }
    }
</script>

<style scoped>

    .common-side-bar {
        position: fixed;
        top: 48px;
        border-right: 1px solid #ddd;
        height: 100%;
        width: 160px;
        background-color: #fff;
        display: inline-block;
    }

    .submenu-side-bar {
        min-width: 100px;
    }
</style>

